<?php

namespace App\Http\Controllers\API;

use App\Http\Controllers\BaseController;
use App\Models\Certificate;
use App\Models\ExhibitorInformation;
use App\Models\ExhibitorMessage;
use App\Models\JournalContent;
use App\Models\MessageProcessCount;
use App\Services\PublicService;
use Illuminate\Http\Request;

class CertificateController extends BaseController
{
    public function add(Request $request)
    {
        $exhibitor_information_id = session()->get('exhibitor_information_id');
        $exhibitor_message_id = session()->get('exhibitor_message_id');

        if (!$exhibitor_message_id) {
            return $this->failure('数据错误，重新登录');
        }

        if (!$exhibitor_information_id) {
            return $this->failure('登录超时，重新登录');
        }

        $certificates_exhibitor_information_number = 0;
        $exhibitor_message = ExhibitorMessage::query()->where('status', 1)->first();
        $exhibitor_information = ExhibitorInformation::query()->find($exhibitor_information_id);

        if ($exhibitor_information) {
            $certificates_exhibitor_information_number = $exhibitor_information->papers;
        }

        $message_process_count_count = MessageProcessCount::query()
            ->where('model_type_id', 3)
            ->where('certificates_exhibitor_information_id', $exhibitor_information_id)
            ->count('id');

        $message_process_count_count_temp = $message_process_count_count + 1;

        if ($message_process_count_count_temp > $certificates_exhibitor_information_number) {
            return $this->failure("只能上传{$certificates_exhibitor_information_number}个");
        }

        $data = $request->all();
        $surplus_number = $certificates_exhibitor_information_number - $message_process_count_count;
        $name = $data['name'];
        $phone = $data['phone'];
        $e_mail = $data['e_mail'];
        $code = $data['code'];
        $type_id = $data['type_id'];

        $data['exhibitor_information_id'] = $exhibitor_information_id;

        $Certificate = Certificate::query()->create($data);

        MessageProcessCount::query()->insert(
            [
                'model_type_id' => 3,//模块类型1，安全责任书，2：会刊内容提交，3：展商证件申请
                'model_id' => $Certificate->id,
                'certificates_exhibitor_information_id' => session('exhibitor_information_id'),
                'exhibitor_message_id' => session('exhibitor_message_id')
            ]
        );

        if ($Certificate) {
            return $this->success($surplus_number);
        } else {
            return $this->failure('失败');
        }
    }

    public function dell(Request $request)
    {
        $exhibitor_information_id = session()->get('exhibitor_information_id');
//        $exhibitor_message_id = session()->get('exhibitor_message_id');
//
//        if (!$exhibitor_message_id) {
//
//            return $this->failure('数据错误');
//        }
        if (!$exhibitor_information_id) {
            return $this->failure('登录超时，重新登录');
        }

        $certificate_id = $request->get('certificate_id');
        $certificate = Certificate::query()->find($certificate_id);

        if (!$certificate) {
            return $this->failure('不存在数据');
        }

        MessageProcessCount::query()->where(['model_id' => $certificate->id])->delete();

        $certificate->delete();

        return $this->success('成功');
    }

    public function show(Request $request)
    {
        $exhibitor_information_id = session()->get('exhibitor_information_id');

        if (!$exhibitor_information_id) {
            return $this->failure('登录超时，重新登录');
        }

        $exhibitor_message_id = session()->get('exhibitor_message_id');
        $certificate_ids = MessageProcessCount::query()
            ->where('model_type_id', 3)
            ->where('certificates_exhibitor_information_id', $exhibitor_information_id)
            ->where('exhibitor_message_id', $exhibitor_message_id)->pluck('model_id')->toArray();

        $certificate['data'] = Certificate::query()->whereIn('id', $certificate_ids)->get()->toArray();
        $certificates_exhibitor_information_number = 0;
        $exhibitor_information = ExhibitorInformation::query()->find($exhibitor_information_id);

        if ($exhibitor_information) {
            $certificates_exhibitor_information_number = $exhibitor_information->papers;
        }

        $message_process_count_count = MessageProcessCount::query()
            ->where('model_type_id', 3)
            ->where('certificates_exhibitor_information_id', $exhibitor_information_id)
            ->count('id');

        $surplus_number = $certificates_exhibitor_information_number - $message_process_count_count;

        $certificate['auxiliary'] = [
            'message_process_count_count' => $message_process_count_count,//上传个数
            'surplus_number' => $surplus_number//剩余个数
        ];

        if ($certificate) {
            return $this->success($certificate);
        } else {
            return $this->failure('数据不存在');
        }
    }

}
